package com.cool.modules.finance.service.family.impl;

import com.cool.core.base.BaseServiceImpl;
import com.cool.modules.finance.entity.family.FinanceUserFamilyEntity;
import com.cool.modules.finance.entity.family.table.FinanceUserFamilyEntityTableDef;
import com.cool.modules.finance.mapper.family.FinanceUserFamilyMapper;
import com.cool.modules.finance.service.family.FinanceUserFamilyService;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class FinanceUserFamilyServiceImpl extends BaseServiceImpl<FinanceUserFamilyMapper, FinanceUserFamilyEntity> implements FinanceUserFamilyService {
    @Override
    public Long getFamilyIdByUserId(Long userId) {
        Optional<FinanceUserFamilyEntity> first = mapper.selectListByQuery(new QueryWrapper().select(FinanceUserFamilyEntity::getFamilyId).where(FinanceUserFamilyEntityTableDef.FINANCE_USER_FAMILY_ENTITY.USER_ID.eq(userId))).stream().findFirst();
        return first.map(FinanceUserFamilyEntity::getFamilyId).orElse(null);
    }

    @Override
    public List<Long> getFamilyUserIdsByUserId(Long userId) {
        Long familyId = getFamilyIdByUserId(userId);
        if (null==familyId){
            return null;
        }
        return list(new QueryWrapper().select(FinanceUserFamilyEntity::getUserId).where(FinanceUserFamilyEntityTableDef.FINANCE_USER_FAMILY_ENTITY.FAMILY_ID.eq(familyId).when(familyId!=null))).stream().map(FinanceUserFamilyEntity::getUserId).toList();
    }
}
